package com.monitor;

import com.monitor.ui.ProtectionPanel;
import com.monitor.ui.ControlPanel;
import com.monitor.ui.ResourcePanel;
import com.monitor.ui.MaintenancePanel;
import com.monitor.ui.TrayIconManager;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class MainFrame extends JFrame {
    private static final String TITLE = "反黄专家系统";
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    private static final Color MAIN_COLOR = new Color(0, 120, 215);
    //private static final Color PANEL_COLOR = new Color(230, 240, 250);
    private static final Color PANEL_COLOR = new Color(255, 0, 0);

    private JPanel leftPanel;
    private JPanel contentPanel;
    private CardLayout cardLayout;

    // 各功能中心面板
    private JPanel protectionPanel;
    private JPanel controlPanel;
    private JPanel resourcePanel;
    private JPanel maintenancePanel;

    // 系统托盘管理器
    private TrayIconManager trayIconManager;

    public MainFrame() {
        initUI();
    }

    private void initUI() {
        setTitle(TITLE);
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(true);

        // 设置整体布局
        setLayout(new BorderLayout());

        // 创建顶部面板
        createTopPanel();

        // 创建左侧导航面板
        createLeftPanel();

        // 创建内容面板
        createContentPanel();

        // 创建底部面板
        createBottomPanel();

        // 初始化系统托盘管理器
        trayIconManager = new TrayIconManager(this);
    }

    private void createTopPanel() {
        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.setBackground(MAIN_COLOR);
        topPanel.setPreferredSize(new Dimension(WIDTH, 40));

        // 添加标题
        JLabel titleLabel = new JLabel(TITLE, JLabel.LEFT);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
        topPanel.add(titleLabel, BorderLayout.WEST);

        // 添加顶部按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setOpaque(false);

        JButton helpButton = createTopButton("帮助");
        JButton settingButton = createTopButton("专家设置");
        JButton minButton = createTopButton("_");
        JButton closeButton = createTopButton("X");

        buttonPanel.add(helpButton);
        buttonPanel.add(settingButton);
        buttonPanel.add(minButton);
        buttonPanel.add(closeButton);

        topPanel.add(buttonPanel, BorderLayout.EAST);

        add(topPanel, BorderLayout.NORTH);
    }

    private JButton createTopButton(String text) {
        JButton button = new JButton(text);
        button.setForeground(Color.WHITE);
        button.setBackground(MAIN_COLOR);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        button.setPreferredSize(new Dimension(60, 30));

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                button.setBackground(new Color(30, 144, 255));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                button.setBackground(MAIN_COLOR);
            }
        });

        if (text.equals("X")) {
            button.addActionListener(e -> {
                // 关闭当前窗口
                dispose();
                
                // 使用JavaFX方式启动DashBoardFrame
                try {
                    // 尝试直接启动JavaFX应用
                    new Thread(() -> {
                        try {
                            Application.launch(DashBoardFrame.class, new String[0]);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }).start();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "启动DashBoardFrame失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        } else if (text.equals("_")) {
            button.addActionListener(e -> {
                if (trayIconManager != null) {
                    trayIconManager.minimizeToTray();
                } else {
                    setState(JFrame.ICONIFIED);
                }
            });
        }

        return button;
    }

    private void createLeftPanel() {
        leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
        leftPanel.setBackground(PANEL_COLOR);
        leftPanel.setPreferredSize(new Dimension(160, 500));
        leftPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.LIGHT_GRAY));

        // 创建导航按钮
        JButton homeBtn = createNavButton("首页", "系统功能概览", new ImageIcon(getClass().getResource("/images/home.png")));
        JButton protectionBtn = createNavButton("防护中心", "实时保护上网环境安全", new ImageIcon(getClass().getResource("/images/protection.png")));
        JButton controlBtn = createNavButton("控制中心", "限制控制设备使用时间", new ImageIcon(getClass().getResource("/images/control.png")));
        JButton resourceBtn = createNavButton("资源中心", "收藏各种教育资源信息", new ImageIcon(getClass().getResource("/images/resource.png")));
        JButton maintenanceBtn = createNavButton("维护中心", "监管更新系统环境", new ImageIcon(getClass().getResource("/images/maintenance.png")));

        leftPanel.add(Box.createVerticalStrut(10));
        leftPanel.add(homeBtn);
        leftPanel.add(Box.createVerticalStrut(10));
        leftPanel.add(protectionBtn);
        leftPanel.add(Box.createVerticalStrut(10));
        leftPanel.add(controlBtn);
        leftPanel.add(Box.createVerticalStrut(10));
        leftPanel.add(resourceBtn);
        leftPanel.add(Box.createVerticalStrut(10));
        leftPanel.add(maintenanceBtn);
        leftPanel.add(Box.createVerticalGlue());

        add(leftPanel, BorderLayout.WEST);
    }

    private JButton createNavButton(String title, String description, Icon icon) {
        JButton button = new JButton();
        button.setLayout(new BorderLayout());
        button.setBackground(PANEL_COLOR);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setMaximumSize(new Dimension(150, 70));
        button.setPreferredSize(new Dimension(150, 70));

        JLabel iconLabel = new JLabel(icon);
        iconLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));

        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
        textPanel.setOpaque(false);

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JLabel descLabel = new JLabel(description);
        descLabel.setFont(new Font("微软雅黑", Font.PLAIN, 10));
        descLabel.setForeground(Color.GRAY);
        descLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

        textPanel.add(titleLabel);
        textPanel.add(descLabel);

        button.add(iconLabel, BorderLayout.WEST);
        button.add(textPanel, BorderLayout.CENTER);

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                button.setBackground(new Color(200, 220, 240));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                button.setBackground(PANEL_COLOR);
            }
        });

        button.addActionListener(e -> {
            cardLayout.show(contentPanel, title);
        });

        return button;
    }

    private void createContentPanel() {
        cardLayout = new CardLayout();
        contentPanel = new JPanel(cardLayout);
        contentPanel.setBackground(Color.WHITE);

        // 创建各功能中心面板
        protectionPanel = createProtectionPanel();
        controlPanel = createControlPanel();
        resourcePanel = createResourcePanel();
        maintenancePanel = createMaintenancePanel();

        contentPanel.add(protectionPanel, "防护中心");
        contentPanel.add(controlPanel, "控制中心");
        contentPanel.add(resourcePanel, "资源中心");
        contentPanel.add(maintenancePanel, "维护中心");
        
        // 默认显示首页
        cardLayout.show(contentPanel, "首页");

        add(contentPanel, BorderLayout.CENTER);
    }


    
    private JPanel createProtectionPanel() {
        // 使用我们实现的ProtectionPanel类
        return new ProtectionPanel();
    }

    private JPanel createControlPanel() {
        // 使用我们实现的ControlPanel类
        return new ControlPanel();
    }

    private JPanel createResourcePanel() {
        // 使用我们实现的ResourcePanel类
        return new ResourcePanel();
    }

    private JPanel createMaintenancePanel() {
        // 使用我们实现的MaintenancePanel类
        return new MaintenancePanel();
    }

    private JPanel createContentTitlePanel(String title) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(MAIN_COLOR);
        panel.setPreferredSize(new Dimension(WIDTH, 40));

        JLabel titleLabel = new JLabel(title, JLabel.CENTER);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));

        panel.add(titleLabel, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createFeatureItem(String title, String description) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 60));
        panel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));

        JPanel iconPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        iconPanel.setBackground(Color.WHITE);
        iconPanel.setPreferredSize(new Dimension(60, 60));

        // 创建一个圆形图标
        JPanel circleIcon = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(new Color(0, 200, 0));
                g2d.fillOval(0, 0, 20, 20);
                g2d.dispose();
            }
        };
        circleIcon.setPreferredSize(new Dimension(20, 20));
        circleIcon.setOpaque(false);

        iconPanel.add(circleIcon);

        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
        textPanel.setBackground(Color.WHITE);

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JLabel descLabel = new JLabel(description);
        descLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        descLabel.setForeground(Color.GRAY);
        descLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

        textPanel.add(Box.createVerticalStrut(10));
        textPanel.add(titleLabel);
        textPanel.add(Box.createVerticalStrut(5));
        textPanel.add(descLabel);
        textPanel.add(Box.createVerticalStrut(10));

        panel.add(iconPanel, BorderLayout.WEST);
        panel.add(textPanel, BorderLayout.CENTER);

        return panel;
    }

    /**
     * 显示指定名称的面板
     * @param panelName 面板名称
     */
    public void showPanel(String panelName) {
        if (cardLayout != null && contentPanel != null) {
            cardLayout.show(contentPanel, panelName);
        }
    }

    private void createBottomPanel() {
        JPanel bottomPanel = new JPanel(new BorderLayout());
        bottomPanel.setBackground(PANEL_COLOR);
        bottomPanel.setPreferredSize(new Dimension(WIDTH, 30));
        bottomPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));

        JPanel leftInfo = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        leftInfo.setOpaque(false);

        JLabel secretLabel = new JLabel("秘密内容");
        JLabel expertLabel = new JLabel("专家模式");
        JLabel techLabel = new JLabel("技术支持");
        JLabel designLabel = new JLabel("网站设计");
        JLabel logLabel = new JLabel("已登录");

        secretLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        expertLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        techLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        designLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        logLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));

        leftInfo.add(secretLabel);
        leftInfo.add(new JLabel("|"));
        leftInfo.add(expertLabel);
        leftInfo.add(new JLabel("|"));
        leftInfo.add(techLabel);
        leftInfo.add(new JLabel("|"));
        leftInfo.add(designLabel);
        leftInfo.add(new JLabel("|"));
        leftInfo.add(logLabel);

        JPanel rightInfo = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
        rightInfo.setOpaque(false);

        JLabel versionLabel = new JLabel("版本号: V5.0");
        versionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));

        rightInfo.add(versionLabel);

        bottomPanel.add(leftInfo, BorderLayout.WEST);
        bottomPanel.add(rightInfo, BorderLayout.EAST);

        add(bottomPanel, BorderLayout.SOUTH);
    }
}
